const api = require('./api');
//操作文件，读写文件
const fs = require('fs');
const path = require('path');
//解析前端发送来的数据
const bodyParser = require('body-parser')
const express = require('express');
const app = express();
// 引入七牛云配置
const qnconfig = require('./config/qnyconfig.js')
// Multer 是一个 node.js 中间件,用于处理 multipart/form-data 类型的表单数据,它主要用于上传文件。
var multer = require('multer')

const mail = require("./email");
const uploader = require('./oss');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(api);
// express.static 用来处理静态资源
app.use(express.static(path.join(__dirname, 'public')));
app.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*")
  res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS")
  res.header("X-Powered-By",' 3.2.1')
  res.header("Content-Type", "application/json;charset=utf-8")
  next()
})



app.post("/api/mail", (req, res) => {
  const content = `
<div style="width: 90%; border: 2px solid lightgreen; margin: 1rem auto; padding: 1rem; text-align: center;">
    <p style="border-bottom: 1px dashed lightgreen;margin: 0;padding-bottom: 1rem; color: lightgreen; font-size: 1.25rem;">Complain Message</p>
<p style="margin: 1rem 0 0;">hello,顽石老板 &#x1f608</p>
<p sytle="margin: 0 0 1rem;">你有一条新投诉待处理</p>
<p style="width: 70%; border-left: 4px solid lightgreen; padding: 1rem; margin: 0 auto 2rem; text-align: left;white-space: pre-line;">主题: ${req.body.subject}
内容: ${req.body.content}
邮箱: ${req.body.address}
    </p>
    <a href="http://localhost:8080:" style="text-decoration: none; background: lightgreen;color: #fff; height: 2rem; line-height: 2rem; padding: 0 1rem; display: inline-block; border-radius: 0.2rem;">回到酒店网站</a>
    </div>
    `;
  const toCustomer = `
<div style="width: 90%; border: 2px solid lightgreen; margin: 1rem auto; padding: 1rem; text-align: center;">
    <p style="border-bottom: 1px dashed lightgreen;margin: 0;padding-bottom: 1rem; color: lightgreen; font-size: 1.25rem;">投诉反馈</p>
<p style="margin: 1rem 0 0;">亲爱的顾客:</p>
<p sytle="margin: 0 0 1rem;">很抱歉我们给您造成的不快体验</p>
<p style="width: 70%; border-left: 4px solid lightgreen; padding: 1rem; margin: 0 auto 2rem; text-align: left;white-space: pre-line;"> 
我们将安排专人跟进您的投诉，力保您的旅行体验
如果三天内我们没有联系您，请拨打028-0999999，进行反馈，谢谢
    </p>
    
    </div>
    `;
    // const toCustomer = '对不起，您的投诉我们已经收到啦，会尽快安排专人跟进哦'
  mail.send("1294553842@qq.com", "有客人投诉啦，快去看看吧", content, res);
  mail.send(req.body.address,"对不起，您的投诉我们已经收到啦，会尽快安排专人跟进哦",toCustomer,res)
  res.status(200).send("send email successfully");
});

app.get('/api/token', (req, res, next) => {
  console.log(qnconfig.uploadToken)
  res.status(200).send(qnconfig.uploadToken)
})




app.listen(8011);
console.log('success listen......');
app.listen(app.get("port"), function () {
  console.log("GetData http://localhost:" + app.get("port"));
});
